using System;
using Data;
using Holoville.HOTween;
using TMPro;
using UnityEngine;

public class UserLevel : MonoBehaviour
{
	private void OnEnable()
	{
		this.Init();
	}

	private void Init()
	{
		this.userLevel.text = DataContainer<Data.Root>.Data.userdata.record.userLevel.ToString();
		this.progress.Percent = (float)DataContainer<Data.Root>.Data.userdata.record.userExp / (float)this.GetNeedExp() * 100f;
	}

	private int GetNeedExp()
	{
		return 15;
		/*if (DataContainer<Data.Root>.Data.userdata.record.userLevel == 1)
		{
			return 15;
		}
		if (DataContainer<Data.Root>.Data.userdata.record.userLevel == 2)
		{
			return 20;
		}
		if (DataContainer<Data.Root>.Data.userdata.record.userLevel <= 3)
		{
			return 25;
		}
		return 25;*/
	}

	public void SetClearExp(Action<bool> callback, bool isSkip = false)
	{
		bool isLevelUp = false;
		this.exp = (float)DataContainer<Data.Root>.Data.userdata.record.userExp / (float)this.GetNeedExp() * 100f;
		if (DataContainer<Data.Root>.Data.userdata.record.userExp + 5 >= this.GetNeedExp())
		{
			DataContainer<Data.Root>.Data.userdata.record.userExp = 0;
			DataContainer<Data.Root>.Data.userdata.record.userLevel++;
			isLevelUp = true;
		}
		else
		{
			DataContainer<Data.Root>.Data.userdata.record.userExp += 5;
			isLevelUp = false;
		}
		DataContainer<Data.Root>.Data.userdata.SaveContext();
		float num = (!isLevelUp) ? ((float)DataContainer<Data.Root>.Data.userdata.record.userExp / (float)this.GetNeedExp() * 100f) : 100f;
		if (!isSkip)
		{
			Sequence sequence = new Sequence(new SequenceParms().Id("USER_LEVEL_ANIMATION"));
			TweenParms p_parms = new TweenParms().Prop("exp", num).Ease(EaseType.EaseOutSine).OnUpdate(delegate()
			{
				this.UpdateExp();
			}).OnComplete(delegate()
			{
				this.Init();
				if (callback != null)
				{
					callback(isLevelUp);
				}
			});
			sequence.Insert(0f, HOTween.To(this, 0.5f, p_parms));
			sequence.Play();
			BMSoundHelper.Play("getExp", false, 1f);
		}
		else
		{
			this.Init();
			if (callback != null)
			{
				callback(isLevelUp);
			}
		}
	}

	private void UpdateExp()
	{
		this.progress.Percent = this.exp;
	}

	public static int GetRewardCoinCount()
	{
		if (DataContainer<Data.Root>.Data.userdata.record.userLevel == 2)
		{
			return 15;
		}
		if (DataContainer<Data.Root>.Data.userdata.record.userLevel == 3)
		{
			return 20;
		}
		if (DataContainer<Data.Root>.Data.userdata.record.userLevel == 4)
		{
			return 25;
		}
		if (DataContainer<Data.Root>.Data.userdata.record.userLevel == 5)
		{
			return 25;
		}
		if (DataContainer<Data.Root>.Data.userdata.record.userLevel <= 6)
		{
			return (DataContainer<Data.Root>.Data.userdata.record.userLevel - 1) * 5;
		}
		return 30;
	}

	public tk2dProgress progress;

	public TextMeshPro userLevel;

	[HideInInspector]
	public float exp;

	private const int LEVEL_EXP = 5;
}
